﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class IndexesDocument : Document
    {
        #region internal
        //Constructor
        internal IndexesDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.Indexes;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + RootDatabase.Name + "." + "indexes.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Indexes"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	#mainBody *.nameColumn {width: 400px; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.tableColumn {width: 200px; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.isClusteredColumn {width: 10%; word-wrap: break-word;}");
                AddCssLine(@"      	#mainBody *.isPrimaryKeyColumn {width: 10%; word-wrap: break-word;}");

                AddBodyLine(@"<div id=""mainBody"">");

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Indexes</h2>");

                #region Table Indexes
                Boolean evenRow = true;
                var tableIndexes = (from schema in _database.Schemas
                                    orderby schema.Name
                                    from table in schema.Tables
                                    orderby table.Name
                                    from index in table.Indexes
                                    orderby index.Name
                                    select index);

                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_tableIndexes"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('tableIndexes');"">Table Indexes</a>");
                AddBodyLine(@"</h2>");
                
                AddBodyLine(@"<div id=""tableIndexes"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn""><col class=""tableColumn"">" +
                    @"<col class=""isClusteredColumn""><col class=""isPrimaryKeyColumn""></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>Table Name</th><th>Is Clustered</th><th>Is Primary Key</th></tr>");

                foreach (SqlServer.Index index in tableIndexes)
                {
                    StringBuilder sb = new StringBuilder();

                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    string href = @"<td>";
                    href += @"<a href=""http://index." + RootDatabase.Name
                        + "." + index.Table.Schema.Name + @"." + index.Table.Name + @".index." +
                        index.Name + @".html"">"
                        + ScrubHtml(index.Name) + @"</a>";
                    href += @"</td>";

                    sb.Append(href);
                    
                    href = @"<td>";
                    if (Business.FilterPreferences.ShowTablesPage)
                    {
                       href += @"<a href=""http://index." + RootDatabase.Name
                           + "." + index.Table.Schema.Name + ".table." + index.Table.Name + @".html"">"
                           + ScrubHtml(index.Table.Schema.Name) + "." + ScrubHtml(index.Table.Name) + @"</a>";
                    }
                    else
                    {
                       href += ScrubHtml(index.Table.Schema.Name) + "." + ScrubHtml(index.Table.Name);
                    }
                    href += @"</td>";
                    sb.Append(href);

                    sb.Append(@"<td>" + index.Type.ToString() + @"</td>");
                    sb.Append(@"<td>" + index.IsPrimaryKey.ToString() + @"</td></tr>");

                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region View Indexes
                var viewIndexes = (from schema in _database.Schemas
                                   orderby schema.Name
                                   from view in schema.Views
                                   orderby view.Name
                                   from index in view.Indexes
                                   orderby index.Name
                                   select index);

                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_viewIndexes"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('viewIndexes');"">View Indexes</a>");
                AddBodyLine(@"</h2>");
                
                AddBodyLine(@"<div id=""viewIndexes"">");
                AddBodyLine(@"<table>");
                AddBodyLine(@"<colgroup><col class=""nameColumn""><col class=""tableColumn""><col class=""isClusteredColumn"">" +
                    @"<col class=""isPrimaryKeyColumn""></colgroup>");
                AddBodyLine(@"<tr><th>Name</th><th>View Name</th><th>Is Clustered</th><th>Is Primary Key</th></tr>");

                foreach (SqlServer.Index index in viewIndexes)
                {
                    StringBuilder sb = new StringBuilder();

                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    string href = @"<td>";
                    href += @"<a href=""http://index." + RootDatabase.Name
                        + "." + index.View.Schema.Name + @"." + index.View.Name + @".index." +
                        index.Name + @".html"">"
                        + ScrubHtml(index.Name) + @"</a>";
                    href += @"</td>";

                    sb.Append(href);

                    href = @"<td>";
                    if (Business.FilterPreferences.ShowViewsPage)
                    {
                       href += @"<a href=""http://index." + RootDatabase.Name
                           + "." + index.View.Schema.Name + ".view." + index.View.Name + @".html"">"
                           + ScrubHtml(index.View.Schema.Name) + "." + ScrubHtml(index.View.Name) + @"</a>";
                    }
                    else
                    {
                       href += ScrubHtml(index.View.Schema.Name) + "." + ScrubHtml(index.View.Name);
                    }
                    href += @"</td>";
                    sb.Append(href);

                    sb.Append(@"<td>" + index.Type.ToString() + @"</td>");
                    sb.Append(@"<td>" + index.IsPrimaryKey.ToString() + @"</td></tr>");

                    AddBodyLine(sb.ToString());
                }

                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion

        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
